/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Model;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;

/**
 *
 * @author PhucAnh
 */
public class Boundary extends Shape{
    Point pointStart;
    Point pointFinish;
    
    int x;
    int y;
    int width;
    int height;
    
    public Boundary() {

    }

    public Boundary(Point pointStart, Point pointFinish) {
        this.pointStart = pointStart;
        this.pointFinish = pointFinish;
        
        width = pointFinish.getX() - pointStart.getX();
        height = pointFinish.getY() - pointStart.getY();
        
//      Chinh sua vi tri hinh oval
        if(width <=0 && height <=0) {
            width = -width;
            height = -height;
            x = pointFinish.getX();
            y = pointFinish.getY();   
        } else if (width <=0 && height >0){
            width = -width;
            x = pointFinish.getX();
            y = pointStart.getY();            
        } else if (width >0 && height <=0){
            height = -height;
            x = pointStart.getX();
            y = pointFinish.getY();  
        } else{
            x = pointStart.getX();
            y = pointStart.getY(); 
        }
       
    }
    
        public void setPoint2(Point pointFinish){
            this.pointFinish = pointFinish;
        
        width = pointFinish.getX() - pointStart.getX();
        height = pointFinish.getY() - pointStart.getY();
        
//      Chinh sua vi tri vien
        if(width <=0 && height <=0) {
            width = -width;
            height = -height;
            x = pointFinish.getX();
            y = pointFinish.getY();   
        } else if (width <=0 && height >0){
            width = -width;
            x = pointFinish.getX();
            y = pointStart.getY();            
        } else if (width >0 && height <=0){
            height = -height;
            x = pointStart.getX();
            y = pointFinish.getY();  
        } else{
            x = pointStart.getX();
            y = pointStart.getY(); 
        }
        }
    
    @Override
    public void drawShape(Graphics g2,BufferedImage img)
    {
        Graphics2D g= (Graphics2D)g2;
        try{
        g.setStroke(new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, 0, new float[]{9}, 0));
        } catch(Exception e){}
        g.setColor(Color.BLACK);
        g.drawRect(x, y, width, height);
    }

    public void toMove(int m,int n)
    {
        x +=m;
        y +=n;
    }
    
    public boolean isContain(Point p){
        if(pointFinish.getX()>=pointStart.getX() && pointFinish.getY()>=pointStart.getY()){
            if(p.getX()>= pointStart.getX() && p.getX()<= pointFinish.getX() 
            && p.getY()>= pointStart.getY() && p.getY()<= pointFinish.getY()){
                return true;
            } else {
                return false;
            }
        } else if (pointFinish.getX()<pointStart.getX() && pointFinish.getY()<pointStart.getY()){
            if(p.getX()>= pointFinish.getX() && p.getX()<= pointStart.getX() 
            && p.getY()>= pointFinish.getY() && p.getY()<= pointStart.getY()){
                return true;
            } else {
                return false;
            }
        } else if (pointFinish.getX()>=pointStart.getX() && pointFinish.getY()<=pointStart.getY()){
            if(p.getX()>= pointStart.getX() && p.getX()<= pointFinish.getX() 
            && p.getY()>= pointFinish.getY() && p.getY()<= pointStart.getY()){
                return true;
            } else {
                return false;
            }
        } else {
            if(p.getX()>= pointFinish.getX() && p.getX()<= pointStart.getX() 
            && p.getY()>= pointStart.getY() && p.getY()<= pointFinish.getY()){
                return true;
            } else {
                return false;
            }
        }
    }
}
